package com.wan.tool;

import android.content.Context;
import android.net.ConnectivityManager;
import android.os.Environment;
import android.util.Log;

import com.google.gson.Gson;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.BaseJsonHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;

import org.apache.commons.io.FileUtils;
import org.apache.http.Header;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

/**
 * Created by wan on 14-2-13.
 */
public class HttpUtils {
    private static AsyncHttpClient client = new AsyncHttpClient();

    public static boolean isWifiConnect(Context context) {
        final ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
//        final android.net.NetworkInfo mobile =connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        return wifi.isAvailable() && wifi.isConnected();

//        if(wifi.isAvailable()||mobile.isAvailable())
//            return true;
//        else
//            return false;
    }


    private static String getTodayPathAndDelOldPath() {
        int day = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
        String todayPath = DBManage.DB_PATH + File.separator + "files" + File.separator + day + File.separator;
        File pathFile = new File(todayPath);
        try {
            if(!pathFile.exists()){
                for (int i = 0; i < day; i++) {
                    File oldFile = new File(DBManage.DB_PATH + File.separator + "files" + File.separator + i + File.separator);
                    FileUtils.deleteDirectory(oldFile);
                }
            }
            FileUtils.forceMkdir(pathFile);


        } catch (IOException e) {
            e.printStackTrace();
        }
        return todayPath;
    }

    public static String getSoundPath(int id) {
        return getTodayPathAndDelOldPath() + id + ".mp3";
    }

    public static String getPicPath(int id) {
        return getTodayPathAndDelOldPath() + id + ".jpg";
    }

    public static void downOneWord(int id) {

        String todayPath = getTodayPathAndDelOldPath();
        final File mp3File = new File(todayPath, id + ".mp3");
        if (mp3File.exists()) {
            return;
        }
        final File jpgFile = new File(todayPath, id + ".jpg");
        if (jpgFile.exists()) {
            return;
        }

        client.get("http://yqbword.duapp.com/api/getaword?v=0.7&id=" + id, new BaseJsonHttpResponseHandler<NetWord>() {
            @Override
            public void onSuccess(int i, Header[] headers, String s, final NetWord netWord) {
                if (netWord != null) {
                    if (netWord.soundUrl != null) {
                        client.get(netWord.soundUrl, new BinaryHttpResponseHandler(new String[]{"audio/mpeg"}) {
                            @Override
                            public void onSuccess(byte[] fileData) {
                                try {
                                    FileUtils.writeByteArrayToFile(mp3File, fileData);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }

                        });
                    }
                    if (netWord.picUrl != null) {
                        client.get(netWord.picUrl, new BinaryHttpResponseHandler() {
                            @Override
                            public void onSuccess(byte[] fileData) {
                                try {
                                    FileUtils.writeByteArrayToFile(jpgFile, fileData);
                                } catch (IOException e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, Throwable throwable, String s, NetWord netWord) {

            }

            @Override
            protected NetWord parseResponse(String s) throws Throwable {
                return new Gson().fromJson(s, NetWord.class);
            }
        });


    }


}
